import { ActionType } from "redux-promise-middleware";
import { DELETE_USER, GET_USER_LIST } from "../types/user";

const initialState = {
    list: [],
    loading: false,
    message: ""
};

export const userReducer = (state = initialState, actions) => {
    const { payload } = actions;
    switch (actions.type) {
        case `${GET_USER_LIST}_${ActionType.Pending}`:
            return {
                ...state,
                loading: true,
                message: ""
            }
        case `${GET_USER_LIST}_${ActionType.Fulfilled}`:
            return {
                ...state,
                loading: false,
                list: payload.list || []
            }
        case `${GET_USER_LIST}_${ActionType.Rejected}`:
            return {
                ...state,
                loading: false,
                message: payload.message
            }
        case `${DELETE_USER}`: return {
            ...state,
            list: state.list.filter(o => o.id != payload)
        }
        default:
            return state;
    }
}